package Utiliypackage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.PeriodFormat;
import org.joda.time.format.PeriodFormatter;

public class UilityClassFroTime {

	String startdatevalue, starttimevalue;

	public UilityClassFroTime(String startdatevalue, String starttimevalue) {
		this.startdatevalue = startdatevalue;
		this.starttimevalue = starttimevalue;

	}

	public String testForeligbleEntry() {

		// finding the system current time and date of system
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
		Date date = new Date();
		String currentDate = sdf.format(date).toString();

		// getting minutes and hours from server
		String str = starttimevalue;
		int hours = 0, minutes = 0;
		DateFormat formatters = new SimpleDateFormat("h:mm a");
		Date dates;
		try {
			dates = formatters.parse(str);

			hours = dates.getHours();
			minutes = dates.getMinutes();
		} catch (ParseException e1) {

			e1.printStackTrace();
		}

		String salesStartDate = startdatevalue + " " + hours + ":" + minutes;

		DateTimeZone timeZone = DateTimeZone.forID("America/Montreal");

		DateTimeFormatter formatter = DateTimeFormat.forPattern(
				"MM/dd/yyyy HH:mm").withZone(timeZone);

		DateTime dateTimeStart = formatter.parseDateTime(currentDate);
		DateTime dateTimeStop = formatter.parseDateTime(salesStartDate);
		Period period = new Period(dateTimeStart, dateTimeStop);
		PeriodFormatter periodFormatter = PeriodFormat.getDefault();
		String output = periodFormatter.print(period);

		// getremainingsalestime(output,finishtimemain);

		String splitdatetime[] = output.split("and");

		for (int i = 0; i < splitdatetime.length; i++)
			splitdatetime[i] = splitdatetime[i].trim();

		String result = "", tagvalue = "";

		for (int i = 0; i < splitdatetime.length; i++) {
			String datetime = splitdatetime[i];
			String[] timePosNegativesplit = datetime.split(",");
			String timePosNegative = timePosNegativesplit[0];
			String[] diftime = timePosNegative.split(" ");
			String dtime = diftime[0];

			String tag = diftime[1];
			if ((tag.equals("day")) && (Integer.parseInt(dtime) > 0)) {

				result = dtime;
				break;

			}

		}
		return result;

	}

}
